package org.song.controller;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.song.annotation.PowerEquals;
import org.song.annotation.RequestTimeRecord;
import org.song.exception.result.AjaxResult;
import org.song.model.dto.LinkDto;
import org.song.model.vo.Link;
import org.song.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 博客友链控制层
 */
@Api(tags = "博客友链控制层")
@Controller
@RequestMapping(value = "/link")
public class LinkController {

    @Autowired( required = true )
    @Qualifier( value = "linkServiceImpl")
    private LinkService linkService;

    /**
     * 查询所有的博客友链
     * @param linkDto
     * @return
     */
    @RequestTimeRecord
    @ApiOperation(value = "查询所有的博客友链" , notes = "查询所有的博客友链")
    @ResponseBody
    @RequestMapping(value = "/listLinks" , method = RequestMethod.GET)
    public AjaxResult<List<Link>> listLinks(@ApiParam(value = "linkDto实例接受查询参数") @NonNull LinkDto linkDto) {
        PageHelper.startPage(linkDto.getPage() , linkDto.getSize());
       return AjaxResult.success(linkService.listLinks(linkDto));
    }

    /**
     * 删除博客友链
     * @param link
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @ApiOperation(value = "删除博客友链" , notes = "删除博客友链")
    @ResponseBody
    @RequestMapping(value = "/deleteLink" , method = RequestMethod.DELETE)
    public AjaxResult<Integer> deleteLink(@ApiParam(value = "接受需要删除的博客友链") @NonNull @RequestBody Link link) {
        return AjaxResult.success(linkService.deleteLink(link));
    }

    /**
     * 增加博客友链
     * @param link
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @ApiOperation(value = "增加博客友链" , notes = "增加博客友链")
    @ResponseBody
    @RequestMapping(value = "/insertLink" , method = RequestMethod.PUT)
    public AjaxResult<Integer> insertLink(@ApiParam(value = "博客友链") @NonNull @RequestBody Link link) {
        return AjaxResult.success(linkService.insertLink(link));
    }

    /**
     * 更新博客友链
     * @param link
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @ApiOperation(value = "更新博客友链" , notes = "更新博客友链")
    @ResponseBody
    @RequestMapping(value = "/updateLink" , method = RequestMethod.POST)
    public AjaxResult<Integer> updateLink(@ApiParam(value = "根据传入的友链信息进行博客友链更新") @NonNull @RequestBody Link link) {
        return AjaxResult.success(linkService.updateLink(link));
    }


}
